/**
 * @file XYXKC
 * @brief XYXKC's ESPONE Mind+ library.
 * @n This is Xiaoying Star's JSON mind+extension library
 *
 * @copyright    XYXKC,2023
 * @copyright    MIT Lesser General Public License
 *
 * @author [email](hyc@xyxedu.cn)
 * @date  2023-11-14
 */
//% color="#ff9933" iconWidth=50 iconHeight=40
namespace json {   
    //% block="Add escalation key[Key] value[Value]" blockType="reporter"
    //% Key.shadow="string" Key.defl=key
    //% Value.shadow="string"  Value.defl=value
    export function PostIdentifier(parameter: any, block: any) {
        let str = parameter.Key.code
        let val = parameter.Value.code
        Generator.addInclude('json', '#include <ArduinoJson.h>');
        Generator.addObject(`getJsonBuffer`, `DynamicJsonBuffer`, `jsonBuffer;`);
        Generator.addObject(`setJson`, `String`, `setJson(String key,String val){
            DynamicJsonBuffer doc;
            JsonObject& object = doc.createObject();
            object["${key}"] = ${val};
            String jsonString;
            object.printTo(jsonString);
            doc.clear();
            return jsonString;

        }`);
        Generator.addCode(`setJson(String(${str}),String(${val}))`);  
    }
    //% block="jsonParsing[STR]" blockType="command"
    //% STR.shadow="string" STR.defl="JSON"
    export function parsingJSON(parameter: any, block: any) {
        let str = parameter.STR.code
        Generator.addInclude('json', '#include <ArduinoJson.h>');
        Generator.addObject(`getJsonBuffer`, `DynamicJsonBuffer`, `jsonBuffer;`);
        Generator.addCode(`JsonObject& object = jsonBuffer.parseObject(${str});`);

    }
    //% block="obtainKey[params]" blockType="reporter"
    //% params.shadow="string" params.defl="key"
    export function getJSON(parameter: any, block: any) {
        let params_val = parameter.params.code
        Generator.addInclude('json', '#include <ArduinoJson.h>');
        Generator.addObject(`getJsonBuffer`, `DynamicJsonBuffer`, `jsonBuffer;`);
        Generator.addCode(`object[${params_val}].as<String>()`);

    }

    //% block="obtainKey1[params1]key2[params2]" blockType="reporter"
    //% params1.shadow="string" params1.defl="data"
    //% params2.shadow="string" params2.defl="a"
    export function getsJSON(parameter: any, block: any) {
        let params_val1 = parameter.params1.code
        let params_val2 = parameter.params2.code
        Generator.addInclude('json', '#include <ArduinoJson.h>');
        Generator.addObject(`getJsonBuffer`, `DynamicJsonBuffer`, `jsonBuffer;`);
        Generator.addCode(`object[${params_val1}][${params_val2}].as<String>()`);

    }

    //% block="obtainMultiLayerKeys[params]" blockType="reporter"
    //% params.shadow="string" params.defl='["data"]["a"]'
    export function getSJSON(parameter: any, block: any) {
        let params_val = parameter.params.code.replace(`"[`, "[").replace(`]"`, "]").replace(/\\/g, "")
        Generator.addInclude('json', '#include <ArduinoJson.h>');
        Generator.addObject(`getJsonBuffer`, `DynamicJsonBuffer`, `jsonBuffer;`);
        Generator.addCode(`object${params_val}.as<String>()`);

    }
}
